package com.sky.service.impl;


import cn.mybatis.mp.core.sql.executor.chain.QueryChain;
import com.sky.dto.ScriptTagRelationsDTO;
import com.sky.entity.ScriptTagRelations;
import com.sky.entity.Scripts;
import com.sky.entity.Tags;
import com.sky.exception.BaseException;
import com.sky.mapper.ScriptTagRelationsMapper;
import com.sky.service.ScriptTagRelationsService;
import com.sky.service.ScriptsService;
import com.sky.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author majun
 * @package com.sky.service.impl
 * @date 2025/6/21 13:38
 */
@Service
public class ScriptTagRelationsServiceImpl implements ScriptTagRelationsService {

    @Autowired
    private ScriptTagRelationsMapper scriptTagRelationsMapper;

    @Autowired
    private ScriptsService scriptsService;

    @Autowired
    private TagsService tagsService;

    /**
     * 保存和修改标签和剧本关系
     *
     * @param scriptTagRelationsDTO 标签和剧本关系
     */
    public void save(ScriptTagRelationsDTO scriptTagRelationsDTO) {

        // 查询剧本
        Scripts scripts = scriptsService.getScriptsById(scriptTagRelationsDTO.getScriptId());

        // 验证剧本不能为空
        if (scripts == null) {
            throw new BaseException("剧本不存在");
        }

        // 验证标签数组不能为空
        if (scriptTagRelationsDTO.getTagId() == null || scriptTagRelationsDTO.getTagId().length == 0) {
            throw new BaseException("标签不能为空");
        }

        // 验证所有标签是否存在
        for (Long tagId : scriptTagRelationsDTO.getTagId()) {
            Tags tags = tagsService.getTagsById(tagId);
            if (tags == null) {
                throw new BaseException("标签不存在，标签ID: " + tagId);
            }
        }

        // 先查询该剧本的所有现有关联记录
        List<ScriptTagRelations> existingRelations = QueryChain.of(scriptTagRelationsMapper)
                .eq(ScriptTagRelations::getScriptId, scriptTagRelationsDTO.getScriptId())
                .list();
        
        // 删除现有关联记录
        for (ScriptTagRelations existingRelation : existingRelations) {
            scriptTagRelationsMapper.deleteById(existingRelation.getId());
        }
        
        // 批量插入新的关联记录
        List<ScriptTagRelations> scriptTagRelationsList = new ArrayList<>();
        
        for (Long tagId : scriptTagRelationsDTO.getTagId()) {
            ScriptTagRelations scriptTagRelations = new ScriptTagRelations();
            scriptTagRelations.setScriptId(scriptTagRelationsDTO.getScriptId());
            scriptTagRelations.setTagId(tagId);
            scriptTagRelationsList.add(scriptTagRelations);
        }
        
        // 批量保存
        scriptTagRelationsMapper.saveBatch(scriptTagRelationsList);
    }

    /**
     * 根据标签id查询
     *
     * @param tagId 标签id
     * @return 标签和剧本关系
     */
    public ScriptTagRelations getByTagId(Long tagId) {

        return QueryChain.of(scriptTagRelationsMapper)
                .eq(ScriptTagRelations::getTagId, tagId)
                .get();

    }

    /**
     * 根据剧本id查询
     *
     * @param scriptId 剧本id
     * @return 标签和剧本关系
     */
    public ScriptTagRelations getByScriptId(Long scriptId) {

        return QueryChain.of(scriptTagRelationsMapper)
                .eq(ScriptTagRelations::getScriptId, scriptId)
                .get();

    }
}
